package leetcode.list;

public class Lc2 {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode ansHead = new ListNode();
        ListNode preNode = ansHead;
        int carry = 0;
        while (l1 != null || l2 != null || carry != 0) {
            int v1 = l1 != null ? l1.val : 0;
            int v2 = l2 != null ? l2.val : 0;
            int val = v1 + v2 + carry;
            ListNode node = new ListNode(val % 10);
            carry = val / 10;
            preNode.next = node;
            preNode = node;
            l1 = l1 != null ? l1.next : null;
            l2 = l2 != null ? l2.next : null;
        }
        return ansHead.next;
    }


}
